﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using Heima.OA.UI.Portal.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Areas.Declare.Controllers
{
    public class BuildingInfoController : BaseController
    {
        public IZS_BuildingInfoService ZS_BuildingInfoService { get; set; }
        // GET: Declare/BuildingInfo
        public string Index(PaginationQuery paginationQuery)
        {
            int total = 0;
            var buildingList = ZS_BuildingInfoService.GetPageEntities(paginationQuery.pagesize, paginationQuery.pageindex, out total, u => u.DelFlag == delflagNormal,
                s => s.CreateTime, true).AsQueryable();

            PaginationData data = new PaginationData() { total = total, rows = JsonHandler.ToJson(buildingList) };
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(data));

            return JsonHandler.ToJson(msg);
        }
        #region query
        public string GetList()
        {
            var buildingList = ZS_BuildingInfoService.GetEntities(u => u.DelFlag == delflagNormal);
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(buildingList));
            return JsonHandler.ToJson(msg);
        }
        #endregion
        #region 添加
        public string Create(ZS_BuildingInfo buildingInfo)
        {
            buildingInfo.DelFlag = delflagNormal;
            buildingInfo.CreateTime = DateTime.Now;

            ZS_BuildingInfoService.Add(buildingInfo);
            JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
            return JsonHandler.ToJson(message);
        }

        #endregion
        #region 编辑
        public string Edit(ZS_BuildingInfo buildingInfo)
        {
            ZS_BuildingInfo editBuilding = ZS_BuildingInfoService.GetModel(buildingInfo.Id);
            string res = string.Empty;
            if (editBuilding == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
            }
            else
            {
                editBuilding.BuildingCode = buildingInfo.BuildingCode;
                editBuilding.BuildingName = buildingInfo.BuildingName;
                editBuilding.Location = buildingInfo.Location;
                editBuilding.Floors = buildingInfo.Floors;
                editBuilding.BuildingStructure = buildingInfo.BuildingStructure;
                editBuilding.HouseUse = buildingInfo.HouseUse;
                editBuilding.ActualityUse = buildingInfo.ActualityUse;
                editBuilding.StructureArea = buildingInfo.StructureArea;
                editBuilding.MappingArea = buildingInfo.MappingArea;
                editBuilding.CompriseArea = buildingInfo.CompriseArea;
                editBuilding.JiDiArea = buildingInfo.JiDiArea;
                editBuilding.PropertyRight = buildingInfo.PropertyRight;
                editBuilding.BuiltTime = buildingInfo.BuiltTime;
                editBuilding.IsObtainNoncommodityRightCertificate = buildingInfo.IsObtainNoncommodityRightCertificate;
                editBuilding.ObtainedCertificateName = buildingInfo.ObtainedCertificateName;
                editBuilding.CertificateCode = buildingInfo.CertificateCode;
                editBuilding.CetificateArea = buildingInfo.CetificateArea;
                editBuilding.NoObtainCertificateBuiltArea = buildingInfo.NoObtainCertificateBuiltArea;
                editBuilding.Remark = buildingInfo.Remark;

                bool success = ZS_BuildingInfoService.Update(editBuilding);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!"));
                }
            }

            return res;
        }
        #endregion
        #region 删除
        public string Delete(int id)
        {
            ZS_BuildingInfo buildingInfo = ZS_BuildingInfoService.GetModel(id);
            string res = string.Empty;
            if (buildingInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除错误!"));
            }
            else
            {
                buildingInfo.DelFlag = (int)DelFlagEnum.Deleted;
                bool success = ZS_BuildingInfoService.Update(buildingInfo);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
                }
            }
            return res;
        }
        #endregion

    }
}